/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>Project: app2022 - Ex8
 * <p>Powered by webrx On 2022-02-08 11:54:13
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ex8 {
    AtomicInteger n = new AtomicInteger();

    public static void main(String[] args) throws InterruptedException {
        var t = new Ex8();

        while (true) {
            var t1 = new Thread(t::work, "A");
            var t2 = new Thread(t::work, "B");
            var t3 = new Thread(t::work, "C");
            t1.start();
            t1.join();
            t2.start();
            t2.join();
            t3.start();
            t3.join();
        }

    }

    void work() {
        var t = Thread.currentThread().getName();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.printf("%s%d ", t, n.incrementAndGet());
        if ("C".equalsIgnoreCase(t)) {
            System.out.println();
        }
    }
}
